package com.pucong.gitee.database.backup.business.service.util.fileService;

import com.pucong.gitee.database.backup.business.service.util.fileService.pool.FtpClientWapper;
import com.pucong.gitee.database.backup.business.service.util.fileService.pool.FtpConnectionManager;
import com.pucong.gitee.database.backup.common.entity.service.vo.ServiceFileTreeVo;
import com.pucong.gitee.database.backup.common.entity.service.ServiceFile;
import com.pucong.gitee.database.backup.configuration.exception.BusinessException;
import com.pucong.gitee.database.backup.utils.FtpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.util.List;

@Slf4j
public class FileOperationServiceFtpImpl extends AbstractFileOperationService {

    private FtpClientWapper ftpClientWapper = null;

    private FTPClient ftpClient = null;

    public FileOperationServiceFtpImpl(String sessionId, ServiceFile serviceFile) {
        super(serviceFile);
        ftpClientWapper = FtpConnectionManager.getConnection(sessionId, serviceFile.getHost(), serviceFile.getPort(), serviceFile.getUsername(), serviceFile.getPassword());
        ftpClient = ftpClientWapper.getFtpClient();
    }

    @Override
    public List<ServiceFileTreeVo> getFileServerCatalogue(String path) {
        List<ServiceFileTreeVo> fileInfoList = null;
        try {
            fileInfoList = FtpUtils.getFileInfoList(ftpClient, path);
        } catch (IOException e) {
            log.error("获取文件目录失败", e);
            throw new BusinessException("获取文件目录失败");
        }
        return fileInfoList;
    }

    @Override
    public boolean uploadFile(InputStream inputStream, String outputPath, String fileName) {
        FtpUtils.upload(ftpClient, inputStream, outputPath, fileName);
        return true;
    }

    @Override
    public boolean addCatalogue(String filePath, String addName) {
        return false;
    }

    @Override
    public boolean deleteFile(String filePath) {
        FtpUtils.deleteFile(ftpClient, filePath);
        return true;
    }

    @Override
    public boolean deleteCatalogue(String filePath) {
        FtpUtils.deleteFile(ftpClient, filePath);
        return true;
    }

    @Override
    public void downloadFile(OutputStream out, String filePath) {
        try {
            FtpUtils.download(ftpClient, out, filePath);
        } catch (IOException e) {
            log.error("下载文件失败", e);
            throw new BusinessException("下载文件失败");
        }
    }

    @Override
    public void lookFile(OutputStream out, String filePath) {
        int idx = filePath.lastIndexOf(File.separator);
        String fileName = filePath.substring(idx + 1, filePath.length());
        String catalogueName = filePath.substring(0, idx + 1);
        final byte[] fileBytesByName = FtpUtils.getFileBytesByName(ftpClient, catalogueName, fileName);
        //进行读写操作
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytesByName)) {
            byte[] content = new byte[1024];
            int length = -1;
            while ((length = byteArrayInputStream.read(content)) != -1) {
                out.write(content, 0, length);
                out.flush();
            }
        } catch (IOException e) {
            log.error("文件流io异常", e);
        } finally {
            //善后工作，关闭各种流
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                log.error("文件流io异常", e);
            }
        }
    }

    @Override
    public void close() {
        ftpClientWapper.close();
    }

    @Override
    public Boolean connectTest() {
        return null;
    }

    @Override
    public boolean rename(String filePath, String oldName, String newName) {
        return false;
    }
}
